<?php
/**
* 验证码生成代码
*
* @version 1.1
* @author  Jerry <Jerry@taomee.com>
**/

include('common.php');
session_start(); 
session_register("login_check_number"); 
error_reporting(E_ALL ^ E_NOTICE);

$line_num   = 10;       //干扰线的数量
$img_height = 50;      //图片高度
$img_width  = 130;     //图片宽度
$str_lenth  = 4;       //验证码数量
$font_size  = 26;      //字体大小
$font_width = 70;      //字体行间距
$font_angle = 20;      //字体角度
$font_type  = array(
					0 => 'TrajanPro-Bold.otf',
					1 => 'StencilStd.otf',
					2 => 'VINERITC.TTF'
					);

if($_GET["act"]== "init")
{
    for($i = 0;$i < $str_lenth;$i++)
    {
        $nmsg .= dechex(rand(0,15));  //随机得到识别码
    }

    $seccode = strtoupper($nmsg);     //转为大写

    $_SESSION['login_check_number'] = $seccode;     //注册 Session

    $aimg = imageCreate($img_width,$img_height);    //生成图片
    ImageColorAllocate($aimg, 245,245,245);         //图片底色，ImageColorAllocate第1次定义颜色PHP就认为是底色了

	//加入干扰线
	for ($i = 0; $i < $line_num; $i++)
	{
		$x1 = rand(0,$img_width);
		$y1 = rand(0,$img_height);
		$x2 = rand(0,$img_width);
		$y2 = rand(0,$img_height);

		imageline($aimg,$x1,$y1,$x2,$y2, imageColorAllocate($aimg,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)));
	}
	
	//加入干扰象素
	for($i=0;$i<120;$i++)   
    {
         imagesetpixel($aimg, rand()%$img_width , rand()%$img_height , imageColorAllocate($aimg,mt_rand(100,255),mt_rand(0,250),mt_rand(0,200)));
    }
	
	//加入识别码
	$code_x   = rand(5,$img_width - $font_size * 3);        //验证码的x轴
	$code_y   = rand($font_size + 10,$img_height - 10);      //验证码的Y轴
	$rand_num = rand(0,2);

    for ($i = 0;$i < $str_lenth;++$i)
    {
        imagettftext($aimg, $font_size, rand(-$font_angle,$font_angle), $code_x + $i*$font_width/$str_lenth,  rand($code_y,$code_y + 5), imageColorAllocate($aimg,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)), "./font/" . $font_type[$rand_num], $seccode[$i]);
    }

    Header("Content-type: image/png");
    Imagepng($aimg);
    ImageDestroy($aimg);
}

?>